package com.taocyb.web.uc.user;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.taocyb.bean.uc.UserBean;
import com.taocyb.commons.uc.entity.User;
import com.taocyb.commons.uc.entity.UserDetail;
import com.taocyb.framework.security.simple.Authentication;
import com.taocyb.framework.utils.BeanMapper;
import com.taocyb.web.BaseLoginController;

/**
 * 用户信息
 */
@Controller
@RequestMapping(value = "/uc/user")
public class UserController extends BaseLoginController{
	
	/**
	 * 用户信息首页
	 */
	@Authentication
	@RequestMapping(value = "")
	public String index(HttpServletRequest request, Model model){
		User user = super.getLoginUser(request);
		UserDetail userDetail = super.userService.findUserDetailByLoginName(user.getLoginName());
		
		model.addAttribute(user);
		model.addAttribute(userDetail);
		return "uc/user/user-index";
	}
	
	/**
	 * 更新用户信息 
	 */
	@Authentication
	@RequestMapping(value = "update", method=RequestMethod.POST)
	public String update(@Valid UserBean bean, HttpServletRequest request, Model model){
		User loginUser = super.getLoginUser(request);

		User user = BeanMapper.map(bean, User.class);
		user.setId(loginUser.getId());
		
		userService.updateUser(user);
		
		return this.index(request, model);
	}
}
